import os
import sys
import win32com.client as client
import getpass

shell = client.Dispatch("WScript.Shell")

def GetShortCut(shortcut):
    return shell.CreateShortCut(shortcut).Targetpath

def createShortCut(filename, lnkname):
    """filename should be abspath, or there will be some strange errors"""
    shortcut = shell.CreateShortCut(lnkname)
    shortcut.TargetPath = filename
    shortcut.save()

def CreateShortCut(filename, lnkname):
    createShortCut(os.path.abspath(filename), lnkname)

def CreateStartupShortCut():
    sysName = os.getenv("SystemDrive")
    username = getpass.getuser()
    startupDir = os.path.join(sysName,"\\users",username,"AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup")
    exe_name = os.path.basename(sys.argv[0])
    lnk_name = os.path.splitext(exe_name)[0] + ".lnk"
    CreateShortCut(sys.argv[0],startupDir + "\\"+lnk_name)

def DeleteStartupShortCut():
    exe_name = os.path.basename(sys.argv[0])
    lnk_name = os.path.splitext(exe_name)[0] + ".lnk"
    sysName = os.getenv("SystemDrive")
    username = getpass.getuser()
    startupDir = os.path.join(sysName,"\\users",username,"AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup")
    if os.path.isfile(startupDir + "\\"+lnk_name):
        os.remove(startupDir + "\\"+lnk_name)
